<template>
  <div class="payApplication">
    <!-- 支付申请记录 -->
    <!-- 搜索部分 -->
    <div class="search">
      <el-button size="mini" type="primary" @click="handleToAddPayment">提交支付资料</el-button>
      <div class="block">
        <el-input
          size="mini"
          placeholder="输入商户ID/商户名称/服务类型"
          clearable
          style="width: 250px"
          v-model="search"
          @keyup.enter.native="handleSearch"
        ></el-input>
        <el-button
          size="mini"
          type="primary"
          @click="handleSearch"
          style="padding: 8px 20px;margin-left: 10px;border: none;color: #626366;background-color: #f1f1f1"
          disable
        >搜索</el-button>
      </div>
    </div>
    <!-- 表格部分 -->
    <div class="tableData">
      <el-table
        style="width: 100%;margin-bottom: 40px"
        :data="tableData"
        @cell-dblclick="handleCopy"
        v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(255, 255, 255, 0.8)"
      >
        <el-table-column align="center" label="序号" type="index" width="50"></el-table-column>
        <!-- <el-table-column align="center" label="商户ID" prop="user_id"></el-table-column> -->
        <el-table-column align="center" min-width="120" label="软件账号" prop="pay_accountno"></el-table-column>
        <el-table-column align="center" min-width="120" label="联系电话" prop="pay_phoneno"></el-table-column>
        <el-table-column align="center" label="支付资料">
          <template slot-scope="scope">
            <a
              :href="hrefUrl[scope.$index]"
              @click="handleDownload(scope.row,scope.$index)"
              style="color: #0094ff;cursor: pointer"
            >资料下载</a>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          min-width="180"
          label="支行信息"
          prop="pay_info_status"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <span>{{scope.row.pay_info_add1}}{{scope.row.pay_info_add2}}{{scope.row.pay_info_add3}}{{scope.row.pay_info_bank}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" min-width="160" label="提交时间" prop="sv_creation_date"></el-table-column>
        <el-table-column align="center" min-width="160" label="处理时间" prop="sv_modification_date"></el-table-column>
        <el-table-column align="center" label="处理状态" prop="pay_info_status"></el-table-column>
        <el-table-column
          align="center"
          min-width="120"
          :show-overflow-tooltip="true"
          label="代理商名称"
          prop="distributor_name"
        ></el-table-column>
        <el-table-column align="center" label="操作" min-width="155">
          <template slot-scope="scope">
            <div v-if="scope.row.pay_info_status == '待处理'">
              <el-button
                size="mini"
                type="primary"
                @click="handleVerified(scope.$index, scope.row)"
                style="padding: 4px 5px"
              >审核通过</el-button>
              <el-button
                size="mini"
                type="danger"
                style="padding: 4px 5px"
                @click="handleAuditFailure(scope.$index, scope.row)"
              >审核失败</el-button>
            </div>
            <div v-else="scope.row.pay_info_status == '已处理'">
              <span>已处理</span>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页器部分 -->
    <Pagination
      :total="total"
      :pageIndex="pageIndex"
      :pageSize="pageSize"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    ></Pagination>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination";
export default {
  data() {
    return {
      // 表格数据
      tableData: [],
      loading: true,
      // 订单状态
      orderStatus: ["已处理", "待处理", "处理失败"],
      // 模糊查找
      search: "",
      // 下载链接
      hrefUrl: [],
      // 分页器的数据
      total: 0, // 总条数
      pageIndex: 1, // 当前页
      pageSize: 10, // 每页数量
      // 身份令牌
      myToken: ""
    };
  },
  components: {
    Pagination
  },
  methods: {
    // 初始化函数
    init() {
      // 获取令牌
      this.myToken = sessionStorage.getItem("myToken");
      // 初始化列表
      this.getOrderData();
    },
    // 点击单元格复制内容
    handleCopy(row, column, cell) {
      let range = document.createRange();
      range.selectNodeContents(cell);
      var selection = document.getSelection();
      selection.removeAllRanges();
      selection.addRange(range);
      document.execCommand("copy");
      this.$message({
        type: "success",
        message: "复制成功"
      });
    },
    // 获取数据的方法
    getOrderData() {
      this.loading = true;
      this.$api
        .getAppletApplicationList(
          this.pageIndex,
          this.pageSize,
          1,
          this.search.trim(),
          this.myToken
        )
        .then(res => {
          this.loading = false;
          const { dataList, total } = res.values;
          if (res.succeed) {
            this.total = total;
            if (dataList) {
              this.tableData = dataList.map(v => {
                // 处理状态
                if (v.pay_info_status == 0) {
                  v.pay_info_status = this.orderStatus[1];
                } else {
                  v.pay_info_status = this.orderStatus[0];
                }
                return {
                  ...v
                };
              });
            }
          }
        });
    },
    handleToAddPayment() {
      this.$router.push("submitPayment");
    },
    // 搜索的功能
    handleSearch() {
      this.pageIndex = 1;
      this.getOrderData();
    },
    // 资料下载
    handleDownload(val, i) {
      var urls = "";
      var pay_info_config = JSON.parse(val.pay_info_config);
      if (pay_info_config.imgs != undefined) {
        Object.keys(pay_info_config.imgs).forEach(function(key) {
          urls += pay_info_config.imgs[key] + ",";
        });
      } else {
        Object.keys(pay_info_config).forEach(function(key) {
          urls += pay_info_config[key] + ",";
        });
      }
      var fileData = new FormData();
      fileData.append("urls", urls);
      if (this.hrefUrl[i] === undefined) {
        this.$api
          .downloadInformation(
            fileData,
            val.sv_wxapp_application_id,
            val.pay_phoneno
          )
          .then(res => {
            const { succeed, values } = res;
            if (succeed) {
              this.hrefUrl[i] = "http://res.decerp.cc" + values;
              window.location.href = "http://res.decerp.cc" + values;
            }
          });
      }
    },
    // 审核功能
    handleVerified(i, val) {
      this.$api
        .paymentApplicationAudit(
          val.sv_wxapp_application_id,
          val.relation_shop_user_id,
          1,
          this.myToken
        )
        .then(res => {
          if (res.succeed) {
            this.$message({
              type: "success",
              message: "审核通过"
            });
            val.pay_info_status = 1;
            this.getOrderData();
          } else {
            this.$message.error("审核失败");
          }
        });
    },
    handleAuditFailure(i, val) {
      this.$api
        .paymentApplicationAudit(
          val.sv_wxapp_application_id,
          val.relation_shop_user_id,
          2,
          this.myToken
        )
        .then(res => {
          this.$message.error("审核失败");
        });
    },
    // 分页器的方法
    handleSizeChange(val) {
      this.pageSize = val;
      this.getOrderData();
    },
    handleCurrentChange(val) {
      this.pageIndex = val;
      this.getOrderData();
    },
    //时间格式化
    dateFormat: function(row, column) {
      var date = row[column.property];
      if (date == undefined) {
        return "";
      }
      return this.$moment(date).format("YYYY-MM-DD HH:mm:ss");
    }
  },
  mounted() {
    // 初始化函数
    this.init();
  }
};
</script>

<style lang="less" scoped>
@deep: ~">>>";
.payApplication {
  background-color: #fff;
  padding: 20px;
  .search {
    margin-bottom: 20px;
    display: flex;
    justify-content: space-between;
  }
  // 更改表头颜色
  .tableData @{deep} .el-table th {
    background-color: #fafafa;
    color: #252525;
    font-weight: 100;
  }
}
</style>